package com.yyfax.ph.base.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.yyfax.ph.R;

public class DialogBase extends Dialog implements OnClickListener, OnDismissListener {
	@BindView(R.id.message_one_top_txt)
	TextView contentOneTopTxt;
	@BindView(R.id.message_one_bottom_txt)
	TextView contentOneBottomTxt;
	@BindView(R.id.content_one_layout)
	LinearLayout contentOneLayout;
	@BindView(R.id.message_two_top_txt)
	TextView contentTwoTxtOne;
	@BindView(R.id.message_two_bottom_txt)
	TextView contentTwoTxtTwo;
	@BindView(R.id.content_two_layout)
	LinearLayout contentTwoLayout;
	@BindView(R.id.confirm_one_txt)
	TextView bottomOneTxt;
	@BindView(R.id.confirm_one_layout)
	FrameLayout bottomOneChooseLayout;
	@BindView(R.id.bottom_one_layout)
	LinearLayout bottomOneLayout;
	@BindView(R.id.cancel_txt)
	TextView bottomTwoTxtOne;
	@BindView(R.id.cancel_layout)
	FrameLayout bottomTwoCancelLayout;
	@BindView(R.id.confirm_txt)
	TextView bottomTwoTxtTwo;
	@BindView(R.id.confirm_layout)
	FrameLayout bottomTwoConfirmLayout;
	@BindView(R.id.bottom_two_layout)
	LinearLayout bottomTwoLayout;

	private boolean dialogFlagOne = false;

	private String contentOneTopText;
	private String contentOneBottomText;

	private String bottomOneContentText;
	private BottomOneClickListener bottomOneClickListener;

	private String bottomTwoTopText;
	private String bottomTwoBottomText;
	private String bottomTwoCancelText;
	private String bottomTwoConfirmText;
	private BottomTwoClickListener bottomTwoClickListener;

	public DialogBase(Context ctx,
			String topOneText, String topTwoText,
			String bottomText, BottomOneClickListener listener) {
		this(ctx);
		dialogFlagOne = true;
		contentOneTopText = topOneText;
		contentOneBottomText = topTwoText;
		bottomOneContentText = bottomText;
		bottomOneClickListener = listener;
	}

	public DialogBase(Context ctx,
			String topOneText, String topTwoText,
			String bottomText, BottomOneClickListener listener,
			boolean cancelAble) {
		this(ctx);
		dialogFlagOne = true;
		contentOneTopText = topOneText;
		contentOneBottomText = topTwoText;
		bottomOneContentText = bottomText;
		bottomOneClickListener = listener;
		setCancelable(cancelAble);
	}

	public DialogBase(Context ctx,
			String topOneText, String topTwoText,
			String bottomOneText, String bottomTwoText,
			BottomTwoClickListener listener) {
		this(ctx);
		dialogFlagOne = false;
		bottomTwoTopText = topOneText;
		bottomTwoBottomText = topTwoText;
		bottomTwoCancelText = bottomOneText;
		bottomTwoConfirmText = bottomTwoText;
		bottomTwoClickListener = listener;
	}

	public DialogBase(Context ctx,
			String topOneText, String topTwoText,
			String bottomOneText, String bottomTwoText,
			BottomTwoClickListener listener,
			boolean cancelable) {
		this(ctx);
		dialogFlagOne = false;
		bottomTwoTopText = topOneText;
		bottomTwoBottomText = topTwoText;
		bottomTwoCancelText = bottomOneText;
		bottomTwoConfirmText = bottomTwoText;
		bottomTwoClickListener = listener;
		setCancelable(cancelable);
	}

	public DialogBase(Context ctx) {
		super(ctx, R.style.progress_style);
		setCancelable(true);
		setCanceledOnTouchOutside(true);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_base_layout);
		ButterKnife.bind(this);
		initView();
	}

	private void initView() {
		if (!TextUtils.isEmpty(contentOneTopText)) {
			contentOneTopTxt.setText(contentOneTopText);
			contentOneTopTxt.setVisibility(View.VISIBLE);
		} else {
			contentOneTopTxt.setVisibility(View.GONE);
		}
		if (!TextUtils.isEmpty(contentOneBottomText)) {
			contentOneBottomTxt.setText(contentOneBottomText);
			contentOneBottomTxt.setVisibility(View.VISIBLE);
		} else {
			contentOneBottomTxt.setVisibility(View.GONE);
		}
		if (!TextUtils.isEmpty(bottomTwoTopText)) {
			contentTwoTxtOne.setText(bottomTwoTopText);
			contentTwoTxtOne.setVisibility(View.VISIBLE);
		} else {
			contentTwoTxtOne.setVisibility(View.GONE);
		}
		if (!TextUtils.isEmpty(bottomTwoBottomText)) {
			contentTwoTxtTwo.setText(bottomTwoBottomText);
			contentTwoTxtTwo.setVisibility(View.VISIBLE);
		} else {
			contentTwoTxtTwo.setVisibility(View.GONE);
		}
		if (!TextUtils.isEmpty(bottomOneContentText)) {
			bottomOneTxt.setText(bottomOneContentText);
			bottomOneTxt.setVisibility(View.VISIBLE);
		} else {
			bottomOneTxt.setVisibility(View.GONE);
		}
		if (!TextUtils.isEmpty(bottomTwoCancelText)) {
			bottomTwoTxtOne.setText(bottomTwoCancelText);
			bottomTwoTxtOne.setVisibility(View.VISIBLE);
		} else {
			bottomTwoTxtOne.setVisibility(View.GONE);
		}
		if (!TextUtils.isEmpty(bottomTwoConfirmText)) {
			bottomTwoTxtTwo.setText(bottomTwoConfirmText);
			bottomTwoTxtTwo.setVisibility(View.VISIBLE);
		} else {
			bottomTwoTxtTwo.setVisibility(View.GONE);
		}
		if (dialogFlagOne) {
			contentOneLayout.setVisibility(View.VISIBLE);
			bottomOneLayout.setVisibility(View.VISIBLE);
			contentTwoLayout.setVisibility(View.GONE);
			bottomTwoLayout.setVisibility(View.GONE);
		} else {
			contentOneLayout.setVisibility(View.GONE);
			bottomOneLayout.setVisibility(View.GONE);
			contentTwoLayout.setVisibility(View.VISIBLE);
			bottomTwoLayout.setVisibility(View.VISIBLE);
		}
	}

	@OnClick({R.id.confirm_one_layout, R.id.confirm_layout, R.id.cancel_layout})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.confirm_one_layout:
				if (bottomOneClickListener == null)
					break;
				bottomOneClickListener.bottomOneClick(this);
				break;
			case R.id.confirm_layout:
				if (bottomTwoClickListener == null)
					break;
				bottomTwoClickListener.onConfirmClick(this);
				break;
			case R.id.cancel_layout:
				if (bottomTwoClickListener == null)
					break;
				bottomTwoClickListener.onCancelClick(this);
				break;
		}
	}

	@Override
	public void onDismiss(DialogInterface dialog) {

	}

	public interface BottomOneClickListener {
		void bottomOneClick(DialogBase dialog);
	}

	public interface BottomTwoClickListener {
		void onCancelClick(DialogBase dialog);

		void onConfirmClick(DialogBase dialog);
	}
}
